package com.wolfalpha.utron.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;

import com.wolfalpha.utron.R;

/**
 * 自定义popupWindow
 *
 * @author wwj
 */
public class MPartnerPopWindow extends PopupWindow implements View.OnClickListener {
    private View conentView;

    private Activity context1;

    public MPartnerPopWindow(final Activity context) {
        this.context1 = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.popupwindow_partner, null);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View  
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽  
        this.setWidth(w);
        // 设置SelectPicPopupWindow弹出窗体的高  
        this.setHeight(LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击  
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态  
        this.update();
        // 实例化一个ColorDrawable颜色为半透明  
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作  
        this.setBackgroundDrawable(dw);
        // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);  
        // 设置SelectPicPopupWindow弹出窗体动画效果  
        this.setAnimationStyle(R.style.PopMainMenuAnimation);

        conentView.findViewById(R.id.ll_pop_main_publish).setOnClickListener(this);
        conentView.findViewById(R.id.ll_pop_main_order).setOnClickListener(this);
        conentView.findViewById(R.id.ll_pop_main_choose).setOnClickListener(this);


//        WindowManager.LayoutParams params = context.getWindow().getAttributes();
//        params.alpha = 0.7f;
//
//        context.getWindow().setAttributes(params);
//        this.setOnDismissListener(new OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                WindowManager.LayoutParams params = context1.getWindow().getAttributes();
//                params.alpha = 1.0f;
//                context1.getWindow().setAttributes(params);
//            }
//        });
    }


    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, 0, 2);
        } else {
            this.dismiss();
        }
    }

    private OnButtonClickedListener mListener;

    public interface OnButtonClickedListener {
        public void click(int which);
    }

    public void setOnButtonClickedListener(OnButtonClickedListener listener) {
        mListener = listener;
    }


    @Override
    public void onClick(View v) {

        if (mListener == null) return;

        switch (v.getId()) {
            case R.id.ll_pop_main_choose:
                //筛选
                mListener.click(3);
                break;
            case R.id.ll_pop_main_order:
                //排序
                mListener.click(2);
                break;
            case R.id.ll_pop_main_publish:
                //发布
                mListener.click(1);
                break;
        }

    }
}